package com.estate.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Activity;
import com.estate.mapper.ActivityMapper;
import com.estate.service.feign.QuartersFeign;
import com.estate.service.service.ActivityService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Resource
    private QuartersFeign quartersFeign;
    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Activity> list = list();
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
        });
        PageInfo<Activity> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult saveActivity(Activity activity) {
        activity.setStatus(0);
        activity.setIsDelete(0);
        log.info("新增activity数据:{}", activity);
        boolean b = save(activity);
        if (!b) {
            return ResultTool.fail(501, "添加活动失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteActivity(String activityId) {
        log.info("删除的activityId:{}", activityId);
        Boolean b = removeById(activityId);
        if (!b) {
            return ResultTool.fail(502,"删除活动失败！");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateActivity(Activity activity) {
        log.info("修改activity数据:{}", activity);
        boolean b = updateById(activity);
        if (!b) {
            return ResultTool.fail(503, "修改活动失败");
        }
        return ResultTool.success("success");
    }
}
